<?php
namespace Home\Controller;

use Think\Model;
class BusinessController extends HomeController{
    
    /* 店铺信息 */
    public function index(){
        !is_login() && $this->referer('User/login',U('Business/index'));
        if(IS_POST){
            $M_shop=M('shop');
            $id=I('id');
            $title=I('title');
            $headpic=I('headpic');
            $signpic=I('signpic');
            $description=I('description');
            
            $data=array();
            $data['title']=I('title');
            $data['headpic']=I('headpic');
            $data['signpic']=I('signpic');
            $data['description']=I('description');
            $data['update_time']=NOW_TIME;
            $map=array('id'=>$id);
            $M_shop->where($map)->save($data) ? $this->ajaxReturn(array('status'=>1)) : $this->error('保存出错');
        }
        else{    
        $M_shop=M('shop');
        $info=$M_shop->where(array('uid'=>UID))->find();        
      
        if(empty($info)){
           $this->redirect('Business/create');           
        }
        
       $info['headpic_path']=get_cover($info['headpic'],'path');
       $info['signpic_path']=get_cover($info['signpic'],'path');        
       $this->assign('info',$info);
       $this->display();
        }
    }
    
    public function create(){   
        !is_login() && $this->referer('User/login',U('Business/create'));
        if(IS_POST){
            $M_shop=M('shop');
            $id=I('id');
            $title=I('title');
            $headpic=I('headpic');
            $signpic=I('signpic');
            $description=I('description');
            
            $data=array();
            $data['title']=I('title');
            $data['headpic']=I('headpic');
            $data['signpic']=I('signpic');
            $data['description']=I('description');
            $data['update_time']=NOW_TIME;
            $data['create_time']=NOW_TIME;
            $data['uid']=UID;
            $data['apply_uid']=UID; 
            $data['status']=1;
            
            get_table_field(UID,'uid','id','shop') && $this->error('不能重复开店');
            
            empty($data['title']) && $this->error('店铺名称不能为空');
             
            !preg_match("/^.{0,60}$/",$data['title']) && $this->error(' 店铺名称60字以内');
            !preg_match("/^.{0,500}$/",$data['description']) && $this->error(' 店铺简价500字以内');
            !preg_match("/^[1-9]\d*$/",$data['headpic']) && $this->error(' 店铺logo参数值错误');
            !preg_match("/^[1-9]\d*$/",$data['signpic']) && $this->error(' 店铺形象图参数值错误');
                     
            $M_shop->add($data) ? $this->ajaxReturn(array('status'=>1,U('Business/index'))) : $this->error('保存出错');
            
        }else{
            get_table_field(UID,'uid','id','shop') && $this->redirect('Business/index');
            $this->display();
        }
      
    }
    
    /* 发布商品 */
    public function addPutong(){
        !is_login() && $this->referer('User/login',U('Business/addPutong'));
         $this->display();
    }
    
    //发布商品ajax处理
    public function addProduct(){
        if(IS_POST){
        $M_shop=M('shop');
        $M_product=M('product');
        $M_product_image=M('product_image');
        
        $shop_id = $M_shop->where(array('uid'=>UID, 'status'=>1))->getfield('id');
        !$shop_id && $this->error('账户没有开通店铺或店铺已被停用');
        
        $id=I('id');
        
        $type=strtolower(I('type'));
        $data['type']=$type;
        $data['title']=I('title');
        $data['uid'] = UID;
        $data['shop_id'] = $shop_id;
        $data['price']=I('price');
        $data['category_id']=I('category_id');
        $data['quantity']=I('quantity',1);
        $data['province_id']=I('province_id');
        $data['city_id']=I('city_id');
        $data['area_id']=I('area_id');
        $data['address']=I('address');
        $data['description']=I('description');
        $data['create_time']=NOW_TIME;
        $data['update_time']=NOW_TIME;
        $data['status']=1; 
        
        !preg_match('/[\x{4e00}-\x{9fa5}A-Za-z0-9-_.\/\s]{4,60}+$/u', $data['title']) && $this->error('商品名称长度必须在5-60个字符之间');
        !get_table_field($data['category_id'], 'id', 'id', 'product_category') && $this->error('商品分类为空或没有找到匹配的分类！');
        !(is_numeric($data['price']) && $data['price'] > 0) && $this->error('请输入商品价格，不能小于1');
        !(preg_match('/[\d]+$/', $data['quantity']) && intval($data['quantity']) >= 0) && $this->error('请输入商品库存，不能小于0');
        !get_table_field($data['province_id'], 'province_id', 'province_id', 'hat_province') && $this->error('请选择发货地的省份');
        !get_table_field($data['city_id'], 'city_id', 'city_id', 'hat_city') && $this->error('请选择发货地的城市');
        !get_table_field($data['area_id'], 'area_id', 'area_id', 'hat_area') && $this->error('请选择发货地的区县');
       
        /*普通开始*/
        if($type=='putong'){                
            $data['security_7days']=I('security_7days');
            $data['security_delivery']=I('security_delivery');
            $data['status']=1;
            $data['in_special']=I('in_special',0);
            $data['in_special_panic']=I('in_special_panic',0);
            $data['in_special_gift']=I('in_special_gift',0);             
            !preg_match('/^[\x{4e00}-\x{9fa5}][\x{4e00}-\x{9fa5}A-Za-z0-9_-]{1,50}+$/u', $data['address']) && $this->error('详细地址长度必须在2~50个字符');
            !getStatus('security_7days', $data['security_7days']) && $this->error('支持7天无理由退货不能为空');
            !getStatus('security_delivery', $data['security_delivery']) && $this->error('承诺发货时间不能为空');
        
        }
        /*普通结束*/
        /*拍品开始*/
        elseif(  $type=='paipin'){
            $data['security_7days']=I('security_7days',0);
            $data['security_delivery']=I('security_delivery',0);            
            $data['cash_deposit'] =  I('post.cash_deposit');
            $data['price_start']=I('price_start');            
            $data['maxprice']=I('maxprice',0);            
            
            !preg_match('/^[\x{4e00}-\x{9fa5}][\x{4e00}-\x{9fa5}A-Za-z0-9_-]{1,50}+$/u', 
                $data['address']) && $this->error('详细地址长度必须在2~50个字符');
            !(is_numeric($data['price_start']))  && $this->error('起拍价错误');
            !($data['cash_deposit'])  && $this->error('保证金错误');
            
            $start_time = strtotime(I('post.start_time') . " " . getStatus('hours', I('post.start_time_hour')) . ":00");
            $end_time = strtotime(I('post.end_time') . " " . getStatus('hours', I('post.end_time_hour')) . ":00");
            $start_time >= $end_time && $this->error('结束时间不能低于初始时间');
            $data['start_time'] = $start_time;
            $data['end_time']=$end_time;             
           
        }
        /*拍品结束*/
        
        /*定制开始*/
        elseif($type=='dingzhi'){               
                $end_time = I('post.end_time');
                $over_day = I('post.over_day');
                $over_persent = I('post.over_persent');
                $data['verify'] = I('post.verify');
                 
                empty($end_time) && $this->error('交货期限不能为空');
                (!is_numeric($over_day) || empty($over_day)) && $this->error('每延迟周期不能为空或不是数字');
                (!is_numeric($over_persent) || empty($over_persent)) && $this->error('补偿百分比不能为空或不是数字');
                 
                $end_time = strtotime($end_time . " 00:00:00");
                $data['end_time'] = $end_time;
                $data['over_day']=I('over_day');
                $data['over_persent']=I('over_persent'); 
            }
            /*定制结束*/
           

            if($id){
                $M_product->where(array('id'=>$id))->save($data);
            }else{
                $id = $M_product->add($data);
            }
            
            
            if($type=='putong'){
                /* 商品专题 */                
                if($data['in_special_panic']){
                    $special_panic = I('post.panic');
                    $data = array(
                        'product_id' => $id,
                        'special_panic_id' => empty($special_panic['special_panic_id']) ? 0 : $special_panic['special_panic_id'],
                        'condition_price' => empty($special_panic['condition_price']) ? 0 : $special_panic['condition_price'],
                        'discount' => empty($special_panic['discount']) ? : $special_panic['discount'],
                        'create_time' => NOW_TIME
                    );
                     
                    $M_product_special_panic = M('product_special_panic');
                    $M_product_special_panic->add($data);
                }
                if($data['in_special_gift']){
                    $M_product_special_gift = M('product_special_gift');
                    $special_gifts = I('post.gift');                     
                    foreach ($special_gifts as $special_gift) {
                        $data = array(
                            'product_id' => $id,
                            'special_gift_id' => $special_gift,
                            'create_time' => NOW_TIME
                        );
                        $M_product_special_gift->add($data);
                    }
                }
            }
            
            if($id){
                $pics = I('post.image_cover_id');
                
                $image_del_map=array();
                $image_del_map['product_id']=$id;
                $image_del_map['cover_id']=array('not in',$pics);
                
                $old=$M_product_image->where(array('product_id'=>$id))->field('cover_id')->select();
                $old_image=array();
                foreach ($old as $olditem){
                    $old_image[]=$olditem['cover_id'];
                }
                foreach ($old_image as $olditem){
                    if(!in_array($olditem, $pics)){
                        $M_product_image->where(array('product_id'=>$id, 'cover_id'=>$olditem))
                        ->delete();
                    }
                }
                
                foreach ($pics as $pic) {
                    if(in_array($pic, $old_image)){
                       
                    }else{                        
                        $M_product_image->add(array('product_id'=>$id, 'cover_id'=>$pic));
                    }
                   // $tem_map=array('product_id'=>$id,'cover_id'=>$pic);
                }
                /*
                $product_image_id=I('product_image_id');
                
                $image_del_map=array();
                $image_del_map['product_id']=$id;
                $image_del_map['id']=array('not in',$product_image_id);
               
                $exit_pics=$M_product_image->where(array('product_id'=>$id))->field('cover_id')->select();
                
                //$result=$M_product_image->where($image_del_map)->delete();
              
                $this->ajaxReturn(is_array($exit_pics));
                exit;
                foreach ($pics as $pic) {
                    if(! in_array($pic, $exit_pics)){
                       // $M_product_image->add(array('product_id'=>$id, 'cover_id'=>$pic));
                       echo '1-';
                    } 
                }
                exit;
                */
                $movie = I('post.movie_id');
                $M_product->where(array('id'=>$id))->save(array('movie'=>$movie));
            }
           // $this->ajaxReturn(array('status'=>11,'url'=>U('Business/addSuccess',array('productid'=>$id))));
        }
    }
    /* 发布拍品 */
    public function addPaipin(){
            !is_login() && $this->referer('User/login',U('Business/addPaipin'));
            $this->display();
        
    }
    
    /* 发布定制商品 */
    public function addDingzhi(){
        !is_login() && $this->referer('User/login',U('Business/addDingzhi'));
        $this->display();
    }
    
 
    
    /* 商品管理 */
    public function putong(){
        !is_login() && $this->referer('User/login',U('Business/putong'));
        $type = I('type');
        
        in_array($type, array('sale', 'store')) || $type = 'sale';
        $M_product=M('product');
        
        //数量统计
        $this->assign('salecount',$M_product->where(array('uid'=>UID,'type'=>'putong','is_show'=>1,'status'=>1))->count()|0);
        $this->assign('storecount',$M_product->where(array('uid'=>UID,'type'=>'putong','is_show'=>1,'status'=>0))->count()|0);
        
        $p=I('p',1);
        $p<=0 && $p=1;
        $pagesize=10;
        $offset=($p-1)*$pagesize;
        
        $map=array();
        $map['type']='putong';
        $map['uid']=UID;
        $map['is_show']=1;
        
        switch ($type){
            case 'sale':
                $map['status']=1;
                break;
            case 'store':
                $map['status']=0;
                break;
        }
        
        $list=$M_product->where($map)->limit($offset,$pagesize)
        ->field('id,price,quantity,sales,title,in_special_panic,in_special_gift,status')->select();
        
        $total=$M_product->where($map)->count();
       
        foreach($list as &$item){
            $item['image_path']=get_cover(
                get_table_field($item['id'],'product_id','cover_id','product_image'),'path');
        }
        
        $this->assign('_list',$list);
        $__PAGE__ = new \Think\Page($total, $pagesize);
        $__PAGE__->lastSuffix = false;
        $__PAGE__->rollPage = 6;
        $__PAGE__->setConfig('prev', '<');
        $__PAGE__->setConfig('next', '>');
        $__PAGE__->setConfig('first', '<<');
        $__PAGE__->setConfig('last', '<span>...</span>');
        $__PAGE__->setConfig('header', '<span>共%TOTAL_PAGE%页</span>');
        $__PAGE__->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %END% %DOWN_PAGE% %HEADER%');
        
        $this->assign('_page', $__PAGE__->show());
        $this->assign('type', $type);
        $this->display();
    }
    
    /* 拍品管理 */
    public function paipin(){
        !is_login() && $this->referer('User/login',U('Business/paipin'));
        $type = I('request.type');        
        in_array($type, array('sale', 'store')) || $type = 'sale';
        
        $M_product=M('product');
        
        //数量统计
        $this->assign('salecount',$M_product->where(array('uid'=>UID,'type'=>'paipin','is_show'=>1,'status'=>1))->count()|0);
        $this->assign('storecount',$M_product->where(array('uid'=>UID,'type'=>'paipin','is_show'=>1,'status'=>0))->count()|0);
        
        $p=I('p',1);
        $p<=0 && $p=1;
        $pagesize=10;
        $offset=($p-1)*$pagesize;
        
        $map=array();
        $map['type']='paipin';
        $map['uid']=UID;
        $map['is_show']=1;
        
        switch ($type){
            case 'sale':
                $map['status']=1;
                break;
            case 'store':
                $map['status']=0;
                break;
        }
        
        $list=$M_product->where($map)->limit($offset,$pagesize)
        ->field('id,price,quantity,sales,title,in_special_panic,in_special_gift,status')->select();
        
        $total=$M_product->where($map)->count();
         
        foreach($list as &$item){
            $item['image_path']=get_cover(
                get_table_field($item['id'],'product_id','cover_id','product_image'),'path');
        }
        
        $this->assign('_list',$list);
        
        $__PAGE__ = new \Think\Page($total, $pagesize);
        $__PAGE__->lastSuffix = false;
        $__PAGE__->rollPage = 6;
        $__PAGE__->setConfig('prev', '<');
        $__PAGE__->setConfig('next', '>');
        $__PAGE__->setConfig('first', '<<');
        $__PAGE__->setConfig('last', '<span>...</span>');
        $__PAGE__->setConfig('header', '<span>共%TOTAL_PAGE%页</span>');
        $__PAGE__->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %END% %DOWN_PAGE% %HEADER%');
        
        $this->assign('_page', $__PAGE__->show());
        $this->assign('type', $type);
        $this->display();
    }
    
    /* 定制商品管理 */
    public function dingzhi(){
        !is_login() && $this->referer('User/login',U('Business/dingzhi'));
        $type = I('request.type');
        
        in_array($type, array('sale', 'store')) || $type = 'sale';
        
        $M_product=M('product');
        
        //数量统计
        $this->assign('salecount',$M_product->where(array('uid'=>UID,'type'=>'dingzhi','is_show'=>1,'status'=>1))->count()|0);
        $this->assign('storecount',$M_product->where(array('uid'=>UID,'type'=>'dingzhi','is_show'=>1,'status'=>0))->count()|0);
        
        $p=I('p',1);
        $p<=0 && $p=1;
        $pagesize=10;
        $offset=($p-1)*$pagesize;
        
        $map=array();
        $map['type']='dingzhi';
        $map['uid']=UID;
        $map['is_show']=1;
        
        switch ($type){
            case 'sale':
                $map['status']=1;
                break;
            case 'store':
                $map['status']=0;
                break;
        }
        
        $list=$M_product->where($map)->limit($offset,$pagesize)
        ->field('id,price,quantity,sales,title,in_special_panic,in_special_gift,status')->select();
        
        $total=$M_product->where($map)->count();
         
        foreach($list as &$item){
            $item['image_path']=get_cover(
                get_table_field($item['id'],'product_id','cover_id','product_image'),'path');
        }
        
        $this->assign('_list',$list);
        
        $__PAGE__ = new \Think\Page($total, $pagesize);
        $__PAGE__->lastSuffix = false;
        $__PAGE__->rollPage = 6;
        $__PAGE__->setConfig('prev', '<');
        $__PAGE__->setConfig('next', '>');
        $__PAGE__->setConfig('first', '<<');
        $__PAGE__->setConfig('last', '<span>...</span>');
        $__PAGE__->setConfig('header', '<span>共%TOTAL_PAGE%页</span>');
        $__PAGE__->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %END% %DOWN_PAGE% %HEADER%');
        
        $this->assign('_page', $__PAGE__->show());
        $this->assign('type', $type);
        $this->display();
    }
    
    /**
     * 编辑普通
     */
    public function EditPutong(){
        !is_login() && $this->referer('User/login',U('Business/putong'));
        $id=I('id');
        $M_product=M('product');
        $M_product_image=M('product_image');
        
        $info=$M_product->find($id);
        $this->assign('identify_price',
            get_table_field($info['category_id'],'product_category_id','price','identify'));
        
        $pics = $M_product_image->where(array('product_id'=>$id))->order('id ASC')->select();
        
        $this->assign('pics', $pics);
        $this->assign('info',$info);
        $this->display('addPutong');
    }
    /**
     * 编辑定制
     */
    public function EditDingzhi(){
        !is_login() && $this->referer('User/login',U('Business/dingzhi'));
        $id=I('id');
        $M_product=M('product');
        $M_product_image=M('product_image');
    
        $info=$M_product->find($id);
        
        $this->assign('identify_price',
            get_table_field($info['category_id'],'product_category_id','price','identify'));
    
        $pics = $M_product_image->where(array('product_id'=>$id))->order('id ASC')->select();
        $this->assign('pics', $pics);
        $this->assign('info',$info);
        $this->display('addDingzhi');
    }
    /**
     * 编辑竞拍
     */
    public function EditPaipin(){
        !is_login() && $this->referer('User/login',U('Business/paipin'));
        $id=I('id');
        $M_product=M('product');
        $M_product_image=M('product_image');
    
        $info=$M_product->find($id);
    
        $this->assign('identify_price',
            get_table_field($info['category_id'],'product_category_id','price','identify'));
    
        $pics = $M_product_image->where(array('product_id'=>$id))->order('id ASC')->select();
        $this->assign('pics', $pics);
        $this->assign('info',$info);
        $this->display('addPaipin');
    }
    /* 订单管理 */
    public function orders(){
        
        !is_login() && $this->referer('User/login', U('Business/orders'));
        $p=I('p',1);
        $p<=0 && $p=1;
        
        $pagesize=5;
        $offset=($p-1)*$pagesize;
        
        $type = I('type');
        $M_order=M('order');
        $keyword=I('order_keyword');
        
        $M_order_product=M('order_product');
        $M_product_image=M('order_image');
              
        $status='';
        
        switch($type){
            case 'pending':
                $status=0;
                break;
            case  'shipping':
                $status=1;
                break;
            case  'receipt':
                $status=2;
                break;
            case 'identify':
                $status=3;
                break;
            case 'shipped':
                $status=4;
                break;
            case 'evaluate':
                $status=5;
                break;
            case 'finish':
                $status=7;
                break;
            case 'close':
                $status=8;
                break;
        }
        /*
        if($keyword){
            if(is_numeric($keyword)){
                $map['order_no']=array('like','%'.$keyword.'%');
            }
        }*/
         $prefix = C('DB_PREFIX');
         $l_table = $prefix.('order');
         $m_table = $prefix.('order_product');
         $r_table = $prefix.('product');
         
         $model=M('order');
         $model = $model->table($l_table.' l')
         ->join($m_table . ' m on m.order_id=l.id')
         ->join($r_table . ' r on r.id=m.product_id');
         
         $options['field'] = array('l.id','l.status','l.order_no','m.order_id',
             'l.price total_price','l.create_time','r.uid');
        
         $options['order']='l.id DESC';
         $options['group']='l.id';
         
         $OPT = new \ReflectionProperty($model,'options');
         $OPT->setAccessible(true);
         
         $join_map=array();
         $static_total_str='';
         if($status !=='' && is_numeric($status)){
             $join_map['l.status'] = $status;
             $static_total_str=' and l.status='.$status ;
         }
         $join_map['r.uid']=UID;
         
         $options['where']=$join_map;         
         $options = array_merge( (array)$OPT->getValue($model), $options );
        
         //$total = $model->where($options['where'])->count();
         
        /*查询------------*/
         $statistics=array();
         $Model2 = new Model();
         $qstr='select count(l.id)   from '.$l_table.' l  join '
                 .$m_table .' m on m.order_id=l.id join '
                 . $r_table . ' r on r.id=m.product_id   where  r.uid='. UID. ' and l.status=';
         
         $qstr2='select count(l.id)   from '.$l_table.' l  join '
             .$m_table .' m on m.order_id=l.id join '
                 . $r_table . ' r on r.id=m.product_id where r.uid='. UID . $static_total_str . ' group by l.id' ;
         $total=count($Model2->query($qstr2 ));
       
         $statistics['pending']=count($Model2->query($qstr . 0 . ' group by l.id') );
         $statistics['shipping']=count($Model2->query($qstr . 1 . ' group by l.id'));
         $statistics['receipt']=count($Model2->query($qstr . 2 . ' group by l.id'));
         $statistics['identify']=count($Model2->query($qstr . 3 . ' group by l.id'));
         $statistics['shipped']=count($Model2->query($qstr . 4 . ' group by l.id'));
         $statistics['evaluate']=count($Model2->query($qstr . 5 . ' group by l.id'));
         $statistics['finish']=count($Model2->query($qstr . 7 . ' group by l.id'));
         $statistics['close']=count($Model2->query($qstr . 8 . ' group by l.id'));
         /*
         $statistics['pending']=$model->where(array('r.uid'=>UID,'l.status'=>0))->count()|0;
         $statistics['shipping']=$model->where(array('r.uid'=>UID,'l.status'=>1))->count()|0;
         $statistics['receipt']=$model->where(array('r.uid'=>UID,'l.status'=>2))->count()|0;
         $statistics['identify']=$model->where(array('r.uid'=>UID,'l.status'=>3))->count()|0;
         $statistics['shipped']=$model->where(array('r.uid'=>UID,'l.status'=>4))->count()|0;
         $statistics['evaluate']=$model->where(array('r.uid'=>UID,'l.status'=>5))->count()|0;
         $statistics['finish']=$model->where(array('r.uid'=>UID,'l.status'=>7))->count()|0;
         $statistics['close']=$model->where(array('r.uid'=>UID,'l.status'=>8))->count()|0;
         */
         $this->assign('statistics',$statistics);
          /*----------------------------------------------------*/
         
         /*查询*/
         $model->setProperty('options',$options);
       
         
         $order_list = $model->limit($offset,$pagesize)->select(); 
         
         foreach ($order_list as &$item){
             $tem_products=$M_order_product->where(array('order_id'=>$item['id']))->select();
             foreach($tem_products as &$tem_item){
                 $tem_item['image_path']=get_cover(
                         get_table_field($tem_item['product_id'],'product_id','cover_id','product_image'),'path');
             }
             $item['products']=$tem_products;                       
         }
        //int_to_string($order_list, array('short_status'=>getStatus('short_order'),'seller_status'=>getStatus('seller_order')));
                   
              
        int_to_string($order_list, array('status'=>getStatus('short_order')));
        $this->assign('order_list',$order_list);
        
        $__PAGE__ = new \Think\Page($total, $pagesize);
        $__PAGE__->lastSuffix = false;
        $__PAGE__->rollPage = 6;
        $__PAGE__->setConfig('prev', '<');
        $__PAGE__->setConfig('next', '>');
        $__PAGE__->setConfig('first', '<<');
        $__PAGE__->setConfig('last', '<span>...</span>');
        $__PAGE__->setConfig('header', '<span>共%TOTAL_PAGE%页</span>');
        $__PAGE__->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %END% %DOWN_PAGE% %HEADER%');
         
        $this->assign('_page', $__PAGE__->show());
        $this->assign('type', $type);
        $this->display();
        
    }
    
    /* 订单详情 */
    public function order(){
        !is_login() && $this->referer('User/login', U('Business/order'));
        
        $order_id=I('request.id');
        
        $M_order_product=M('order_product');
        $M_order=M("order");
        $M_payment_log=M('payment_log');
        $M_order_shipping=M('order_shipping');
        
        $prefix = C('DB_PREFIX');
        $l_table = $prefix.('product');
        $r_table = $prefix.('order_product');
         
        $model=D('product');
        $model = $model->table($l_table.' l')
        ->join($r_table . ' r on r.product_id=l.id');
         
        $options['field'] = array('r.product_title','l.security_7days','l.price unit_price',
            ' l.security_delivery',
            'l.shop_id','r.product_id', 'r.price unit_price','r.quantity',
            'r.identify_price','r.product_attr','r.is_comment','r.return_status');
        $OPT = new \ReflectionProperty($model,'options');
        $OPT->setAccessible(true);
        $join_map=array();
        
        $order_info=$M_order->field('id order_id, order_no,status,create_time,price total_price,province_id,city_id,consignee,address,mobile')->find($order_id);
        
        $short_order=getStatus('short_order');
        $seller_orderr=getStatus('seller_order');
        $order_info['short_order']=$short_order[$order_info['status']];
        $order_info['seller_order']=$seller_orderr[$order_info['status']];
        if((int)$order_info['status']>=1){
            $tem_payment=$M_payment_log->where(array('order_id'=>$order_id))->find();
            $order_info['pay_time']=$tem_payment['create_time'];
             
            $order_info['transaction_no']=$tem_payment['transaction_no'];
        }
        
        if((int)$order_info['status']>=2){
            $tem_order_shipping=$M_order_shipping->where(array('order_id'=>$order_id,'type'=>'seller'))->find();
            $shipping_info=array();
            $shipping_info['shipping_code']=$tem_order_shipping['code'];
            $shipping_info['shipping_time']=$tem_order_shipping['create_time'];
            $shipping_info['shipping_company']=get_table_field($tem_order_shipping['shipping_code'],'code','title','shipping');
            $this->assign('shipping_info',$shipping_info);
        }
         
        $order_info['address_full']=
        get_table_field($order_info['province_id'],'province_id','province','hat_province')
        .get_table_field($order_info['city_id'],'city_id','city','hat_city')
        .get_table_field($order_info['area_id'],'area_id','area','hat_area').$order_info['address'];
         
        $join_map['r.order_id'] = $order_id;
        $options['where']=$join_map;
         
        $options = array_merge( (array)$OPT->getValue($model), $options );
        $model->setProperty('options',$options);
         
        $Product_list = $model->select();
        $identify_price=0;
        foreach ($Product_list as &$pro_item){
            $identify_price+=(double)$pro_item['identify_price'];
            $pro_item['image']=get_cover(get_table_field($pro_item['product_id'],'product_id','cover_id','product_image'),'path');
        }
        $order_info['identify_price']=$identify_price;
        $order_info['products']=$Product_list;
         
        $type='';       
        switch((int)$order_info['status']){
            case 0:
                $type='pending';
                break;
            case  1:
                $type='shipping';
                break;
            case  2:
                $type='receipt';
                break;
            case  3:
                $type='identify';
                break;
            case  4:
                $type='shipped';
                break;
            case 5:
                $type='evaluate';
                break;
            case 7:
                $type='finish';
                break;
            case 8:
                $type='close';
                break;
        }
        $this->assign('type', $type);
        $this->assign('info',$order_info);
         
        $M_order_shipping = M('order_shipping');
        $seller_shipping = $M_order_shipping->where(array('order_id'=>$order_id, 'type'=>'seller'))->find();
        $admin_shipping = $M_order_shipping->where(array('order_id'=>$order_id, 'type'=>'admin'))->find();
        $seller_shipping['shipping'] = getExpress($seller_shipping['shipping_code'], $seller_shipping['code']);
        $admin_shipping['shipping'] = getExpress($admin_shipping['shipping_code'], $admin_shipping['code']);
        $this->assign('seller_shipping', $seller_shipping);
        $this->assign('admin_shipping', $admin_shipping);
        
        
        $this->display();
    }
    
    /* 发货到聚真宝 */
    public function shipping(){
        
        if(IS_POST){
            $orderid=I('orderid');
            $shipping_code=I('shipping_code');
            $cover_id=I('cover_id');
            $code=I('code');            
         
            $M_order=M('order');
            $M_order_shipping=M('order_shipping');
            $info=$M_order->find($orderid);
            empty($info)  && $this->error('订单不存在');
            
            is_array($cover_id) && $cover_id=implode(',',$cover_id);
            
            
            $data=array();
            $data['shipping_code']=$shipping_code;
            $data['order_id']=$orderid;
            $data['type']='seller';
            $data['code']=$code;
            $data['create_time']=NOW_TIME;
            $data['cover_ids']=$cover_id;
            
            $result=$M_order_shipping->add($data);
            if($result){
                $result =$M_order->where(array('id'=>$orderid))->save(array('status'=>2,'update_time'=>NOW_TIME));
            }
            if($result){
                addMessage(1,'发货通知','您的订单'.$info['order_no'].'已发货,快递公司是'.$data['shipping_code']
                    .',快递单号是'.$data['code'],$info['uid']);
                $this->success('发货成功',U('Business/orders'));
            }else{
                $this->error('发货失败');
            }
                
              
        }else{
            !is_login() && $this->referer('User/login', U('Business/orders'));
            $order_id=I('orderid');
            $M_order=M('order');
            $M_order_product=M('order_product');
            $order_info=$M_order->find($order_id);
            
            $order_info['address_full']=
            get_table_field($order_info['province_id'],'province_id','province','hat_province')
            .get_table_field($order_info['city_id'],'city_id','city','hat_city')
            .get_table_field($order_info['area_id'],'area_id','area','hat_area').$order_info['address'];
            
            $Product_list =  $M_order_product->where(array('order_id'=>$order_id))->field('product_id')->select();
            $identify_price=0;
            foreach ($Product_list as &$pro_item){
                $identify_price+=(int)$pro_item['identify_price'];
                $pro_item['image']=get_cover(get_table_field($pro_item['product_id'],'product_id',
                    'cover_id','product_image'),'path');
            }
            
            $order_info['identify_price']=$identify_price;
            $order_info['product']=$Product_list;
            $this->assign('info',$order_info);
            $ship_list=M('shipping')->select();
            $this->assign('shiping',$ship_list);
            $this->display();
        }
    }
    
    /* 服务大厅 */
    public function service(){
        $this->display();
    }
    
    /* 聚真宝商学院 */
    public function tech(){
        $type = I('request.type');        
        in_array($type, array('question', 'video')) || $type = 'question';        
        $l_type='doc';
        switch ($type) {
            case 'question':
                $l_type='doc';              
                break;
            case 'video':
                $l_type='video';              
                break;
            default:
                $l_type='doc';               
                break;
        }
        
        $p=I('p',1);
        $p<=0 && $p=1;
        $pagesize=10;
        $offset=($p-1)*$pagesize;
        
        $M_learning_center=M('learning_center');
        $map=array('type'=>$l_type);
        
        $total=$M_learning_center->where($map)->count();
        $list=$M_learning_center->limit($offset,$pagesize)->where($map)->select();
        foreach($list as &$item){
            if($item['movie']){
                $item['thumb_cover_id']=get_movie($item['movie'],'thumb_cover_id');
            }
        }
        
        $this->assign('list',$list);        
        $__PAGE__ = new \Think\Page($total, $pagesize);
        $__PAGE__->lastSuffix = false;
        $__PAGE__->rollPage = 6;
        $__PAGE__->setConfig('prev', '<');
        $__PAGE__->setConfig('next', '>');
        $__PAGE__->setConfig('first', '<<');
        $__PAGE__->setConfig('last', '<span>...</span>');
        $__PAGE__->setConfig('header', '<span>共%TOTAL_PAGE%页</span>');
        $__PAGE__->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %END% %DOWN_PAGE% %HEADER%');
        
        $this->assign('_page', $__PAGE__->show());
        $this->assign('type', $type);  
        
        switch ($type) {
            case 'question':
                $this->display('tech_question');
                break;
            case 'video':
                $this->display('tech_video');
                break;
            default:
                $this->display('tech_question');
                break;
        }
    }
    
    /**
     * 学院 详细
     * @param int id
     */
    public function Learning(){
        $id=I('id');
        $M_l_c = M('learning_center');
        $info=$M_l_c->find($id);
        
        if($info['movie']){
                $info['movie_path'] = __ROOT__ . '/Uploads/Movie/' . get_movie($info['movie'], 'savepath')
                . get_movie($info['movie'], 'savename');
                $info['movie_mime'] = get_movie($info['movie'], 'mime');
                
                $info['thumb_cover_id']=get_movie($info['movie'],'thumb_cover_id');
                $info['thumb_cover_path']=get_cover( $info['thumb_cover_id'],'path');
        }else if($info['resources_link']){
            $info['movie_path']=$info['resources_link'];
        }
       
        $this->assign('info',$info);
        $this->display();
        
    }
    /**
     * ajax 获取鉴定费用
     */
    function ajaxgetIdentify($category_id=0){
        $identify=get_table_field($category_id,'product_category_id','price','identify')|0;
        $this->ajaxReturn($identify);
    }
    
    /* 发布成功 */
    public function addSuccess(){
        $this->display();
    }
    
    /* 卖家收银台 */
    public function cash(){
        !is_login() && $this->referer('User/login', U('Business/order'));
        
        $p=I('p',1);
        $pagesize=8;
        $p<=0 && $p=1;
        $offset=($p-1)*$pagesize;
        
          $prefix = C('DB_PREFIX');
         $l_table = $prefix.('order_product');
         $r_table = $prefix.('product');
         $f_table = $prefix.('order');
         $model=D('order_product');
         $model = $model->table($l_table.' l')
         ->join($r_table . ' r on r.id=l.product_id')
         ->join($f_table . ' f on f.id=l.order_id');
          
        $options['field'] = array('l.id','l.identify_price','l.order_id',
                'l.price','l.product_id','l.create_time','r.title','r.uid',
                'f.order_no','f.status');
         
         $options['order']='l.id DESC';
         
         $join_map=array();   
         $join_map['r.uid'] = UID;
         $join_map['f.status']=array('between',array(1,6));
         $OPT = new \ReflectionProperty($model,'options');
         $OPT->setAccessible(true);
         $options['where']=$join_map;          
         
         $options = array_merge( (array)$OPT->getValue($model), $options );
         $total = $model->where($options['where'])->count();
         //总数
         $model->setProperty('options',$options);
          
         $list = $model->limit($offset,$pagesize)->select();
         $totalprice=0;
         
         foreach ($list as &$pro_item){
             
             $realPrice=(double)$pro_item['price']-(double)$pro_item['identify_price'];             
             $pro_item['realprice']=$realPrice;
             $totalprice+=$realPrice;
             $pro_item['image_path']=get_cover(get_table_field($pro_item['product_id'],'product_id','cover_id','product_image'),'path');
         }
    
         $this->assign('totalprice',$totalprice);
         $this->assign('list',$list);
         $__PAGE__ = new \Think\Page($total,$pagesize);
         $__PAGE__->lastSuffix = false;
         $__PAGE__->rollPage = 6;
         $__PAGE__->setConfig('prev', '<');
         $__PAGE__->setConfig('next', '>');
         $__PAGE__->setConfig('first', '<<');
         $__PAGE__->setConfig('last', '<span>...</span>');
         $__PAGE__->setConfig('header', '<span>共%TOTAL_PAGE%页</span>');
         $__PAGE__->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %END% %DOWN_PAGE% %HEADER%');
         
         $this->assign('_page', $__PAGE__->show());
         $this->display();
    }
    
    /* 鉴定服务费 */
    public function identify(){
        !is_login() && $this->referer('User/login', U('Business/order'));
        
        $p=I('p',1);
        $pagesize=8;
        $p<=0 && $p=1;
        $offset=($p-1)*$pagesize;
        
        $prefix = C('DB_PREFIX');
        $l_table = $prefix.('order_product');
        $r_table = $prefix.('product');
        $f_table = $prefix.('order');
        $model=D('order_product');
        $model = $model->table($l_table.' l')
        ->join($r_table . ' r on r.id=l.product_id')
        ->join($f_table . ' f on f.id=l.order_id');
        
        $options['field'] = array('l.id','l.identify_price','l.order_id',
                'l.price','l.product_id','l.create_time','r.title','r.uid',
                'f.order_no','f.status');
         
        $options['order']='l.id DESC';
         
        $join_map=array();
        $join_map['r.uid'] = UID;
        $join_map['f.status']=array('between',array(1,6));
        $OPT = new \ReflectionProperty($model,'options');
        $OPT->setAccessible(true);
        $options['where']=$join_map;
         
        $options = array_merge( (array)$OPT->getValue($model), $options );
        $total = $model->where($options['where'])->count();
        //总数
        $model->setProperty('options',$options);
        
        $list = $model->limit($offset,$pagesize)->select();
        $totalprice=0;
         int_to_string($list,array('status'=>getStatus('short_order')));
        foreach ($list as &$pro_item){
            $totalprice+=(double)$pro_item['identify_price'];
            $pro_item['image_path']=get_cover(get_table_field($pro_item['product_id'],'product_id','cover_id','product_image'),'path');
        }
        
        $this->assign('totalprice',$totalprice);
        $this->assign('list',$list);
        $__PAGE__ = new \Think\Page($total,$pagesize);
        $__PAGE__->lastSuffix = false;
        $__PAGE__->rollPage = 6;
        $__PAGE__->setConfig('prev', '<');
        $__PAGE__->setConfig('next', '>');
        $__PAGE__->setConfig('first', '<<');
        $__PAGE__->setConfig('last', '<span>...</span>');
        $__PAGE__->setConfig('header', '<span>共%TOTAL_PAGE%页</span>');
        $__PAGE__->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %END% %DOWN_PAGE% %HEADER%');
         
        $this->assign('_page', $__PAGE__->show());
        $this->display();
    }
    
}